using System.Data;
using MySqlConnector;

namespace Front._1125;

public partial class BookForm : Form
{
    public BookForm()
    {
        InitializeComponent();
    }

    private void BookForm_Load(object sender, EventArgs e)
    {
        //查询所有图书
        GetAllBooks();

        //查询所有分类
        GetAllCategories();
    }

    private void GetAllBooks()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        string sql = "SELECT book_id,isbn,book_name FROM book_info";
        //创建MySqlDataAdapter
        MySqlDataAdapter  mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        //创建BookSet
        DataSet dateSet = new DataSet();
        //填充数据
        mySqlDataAdapter.Fill(dateSet, "book_info");
        this.dataGridView1.DataSource = dateSet.Tables["book_info"];
    }

    private void GetAllCategories()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        string sql = "SELECT distinct book_category FROM book_info";
        //创建MySqlDataAdapter
        MySqlDataAdapter  mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        //创建BookSet
        DataSet dataSet = new DataSet();
        //填充数据
        mySqlDataAdapter.Fill(dataSet, "book_category");
        if (dataSet.Tables["book_category"].Rows.Count > 0)
        {
            DataTable categoryTable = dataSet.Tables["book_category"];
            // 插入一行全部
            DataRow row = categoryTable.NewRow();
            row[0] = "全部";
            categoryTable.Rows.InsertAt(row,0);
            comboBox1.DataSource = categoryTable;
            comboBox1.DisplayMember = categoryTable.Columns[0].ColumnName;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        AddBookForm addBookForm = new AddBookForm();
        DialogResult result = addBookForm.ShowDialog();
        if (result == DialogResult.OK)
        {
            //刷新
            GetAllBooks();
        }
    }

    private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //获取选中的行
        DataGridViewRow row = this.dataGridView1.SelectedRows[0];
        int bookId = (int)row.Cells[0].Value;
        DialogResult result = MessageBox.Show("确定删除词条数据吗？","警告",MessageBoxButtons.OKCancel);
        if (result == DialogResult.OK)
        {
            //删除
            using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
            connection.Open();

            string sql = "delete from `book_info` where book_id=@book_id;";
            using var command = new MySqlCommand(sql, connection);
            //设置参数
            command.Parameters.AddWithValue("book_id",bookId);
            int count = command.ExecuteNonQuery();
            if (count > 0)
            {
                //成功
                MessageBox.Show("删除成功");
                //刷新
                GetAllBooks();
            }
            else
            {
                //失败
                MessageBox.Show("删除失败");
            }
        }
        else
        {
        }
    }
}